package com.jeff.android.lab.module.widget.webview;

import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;
import com.jeff.android.lab.util.RouterUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author: dingfeng
 * @date: 2019-09-20
 * @since: 1.0.0
 **/
public class WebViewPoolFragment extends BaseFragment {
    @BindView(R.id.tv_init_time)
    TextView tv_init_time;

    @BindView(R.id.webview_container)
    FrameLayout mWebViewContainer;

    WebView mWebView;

    @Override
    protected int getRootLayout() {
        return R.layout.webview_pool_fragment;
    }

    @Override
    protected void initView() {
        long start = System.currentTimeMillis();
        mWebView = WebViewPool.getInstance().obtain(getActivity());
        mWebViewContainer.addView(mWebView);
        tv_init_time.setText(String.format("初始化时间：%s", (System.currentTimeMillis() - start)));
    }


    @Override
    protected void initData() {
        mWebView.loadUrl("https://www.baidu.com/");
    }

    @OnClick(R.id.btn_open)
    void open() {
        RouterUtil.launchFragmentActivity(getContext(), WebViewPoolFragment.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        WebViewPool.getInstance().recycle(mWebView);
    }

    public void handleBack() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            getActivity().finish();
        }
    }

}
